package com.qpx.goodsservice.service;


import com.qpx.common.dto.ItemDto;
import com.qpx.goodsservice.dao.Itemdao;
import com.qpx.goodsservice.entity.TItemEnity;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ItemService {



    @Autowired
    Itemdao itemdao;
    //UserService
    public Page<TItemEnity> getUserList(ItemDto itemDto){

        Pageable pageable = PageRequest.of(itemDto.getPageNo()-1,itemDto.getPageSize(), Sort.Direction.DESC,"id");
        Specification<TItemEnity> specification = new Specification<TItemEnity>() {
            @Override
            public Predicate toPredicate(Root<TItemEnity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> p = new ArrayList<>();
                if (StringUtils.isNotBlank(itemDto.getItemBrand())){
                    Predicate username = criteriaBuilder.equal(root.get("itemBrand"),itemDto.getItemBrand());
                    p.add(username);
                }
                if (StringUtils.isNotBlank(itemDto.getItemName())){
                    Predicate name = criteriaBuilder.like(root.get("itemName"), "%".concat(itemDto.getItemName()).concat("%"));
                    p.add(name);
                }
                if (itemDto.getItemPrice()!=null){
                    Predicate sex = criteriaBuilder.gt(root.get("itemPrice"), itemDto.getItemPrice());
                    p.add(sex);
                }
                if (StringUtils.isNotBlank(itemDto.getStartTime())&&StringUtils.isNotBlank(itemDto.getEndTime())){
                    try {
                        Date begin = DateUtils.parseDate(itemDto.getStartTime().concat(" 00:00:00"),new String[]{"yyyy-MM-dd HH:mm:ss"});
                        Date end = DateUtils.parseDate(itemDto.getEndTime().concat(" 23:59:59"),new String[]{"yyyy-MM-dd HH:mm:ss"});
                        Predicate createTime = criteriaBuilder.between(root.get("createTime"), begin, end);
                        p.add(createTime);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                return criteriaBuilder.and(p.toArray(new Predicate[p.size()]));
            }
        };
        return itemdao.findAll(specification,pageable);
    };

    public TItemEnity getItemById(Integer id) {
        TItemEnity tItemEnity = itemdao.findById(id).get();
        return tItemEnity;
    }
}
